<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>User GC (GNU Compiler Collection (GCC) Internals)</title>

<meta name="description" content="User GC (GNU Compiler Collection (GCC) Internals)">
<meta name="keywords" content="User GC (GNU Compiler Collection (GCC) Internals)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Option-Index.html" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Type-Information.html" rel="up" title="Type Information">
<link href="GGC-Roots.html" rel="next" title="GGC Roots">
<link href="Inheritance-and-GTY.html" rel="prev" title="Inheritance and GTY">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
-->
</style>


</head>

<body lang="en">
<div class="section-level-extent" id="User-GC">
<div class="nav-panel">
<p>
Next: <a href="GGC-Roots.html" accesskey="n" rel="next">Marking Roots for the Garbage Collector</a>, Previous: <a href="Inheritance-and-GTY.html" accesskey="p" rel="prev">Support for inheritance</a>, Up: <a href="Type-Information.html" accesskey="u" rel="up">Memory Management and Type Information</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h3 class="section" id="Support-for-user_002dprovided-GC-marking-routines"><span>23.3 Support for user-provided GC marking routines<a class="copiable-link" href="#Support-for-user_002dprovided-GC-marking-routines"> &para;</a></span></h3>
<a class="index-entry-id" id="index-user-gc"></a>
<p>The garbage collector supports types for which no automatic marking
code is generated.  For these types, the user is required to provide
three functions: one to act as a marker for garbage collection, and
two functions to act as marker and pointer walker for pre-compiled
headers.
</p>
<p>Given a structure <code class="code">struct GTY((user)) my_struct</code>, the following functions
should be defined to mark <code class="code">my_struct</code>:
</p>
<div class="example smallexample">
<pre class="example-preformatted">void gt_ggc_mx (my_struct *p)
{
  /* This marks field 'fld'.  */
  gt_ggc_mx (p-&gt;fld);
}

void gt_pch_nx (my_struct *p)
{
  /* This marks field 'fld'.  */
  gt_pch_nx (tp-&gt;fld);
}

void gt_pch_nx (my_struct *p, gt_pointer_operator op, void *cookie)
{
  /* For every field 'fld', call the given pointer operator.  */
  op (&amp;(tp-&gt;fld), NULL, cookie);
}
</pre></div>

<p>In general, each marker <code class="code">M</code> should call <code class="code">M</code> for every
pointer field in the structure.  Fields that are not allocated in GC
or are not pointers must be ignored.
</p>
<p>For embedded lists (e.g., structures with a <code class="code">next</code> or <code class="code">prev</code>
pointer), the marker must follow the chain and mark every element in
it.
</p>
<p>Note that the rules for the pointer walker <code class="code">gt_pch_nx (my_struct
*, gt_pointer_operator, void *)</code> are slightly different.  In this
case, the operation <code class="code">op</code> must be applied to the <em class="emph">address</em> of
every pointer field.
</p>
<ul class="mini-toc">
<li><a href="#User_002dprovided-marking-routines-for-template-types" accesskey="1">User-provided marking routines for template types</a></li>
</ul>
<div class="subsection-level-extent" id="User_002dprovided-marking-routines-for-template-types">
<h4 class="subsection"><span>23.3.1 User-provided marking routines for template types<a class="copiable-link" href="#User_002dprovided-marking-routines-for-template-types"> &para;</a></span></h4>
<p>When a template type <code class="code">TP</code> is marked with <code class="code">GTY</code>, all
instances of that type are considered user-provided types.  This means
that the individual instances of <code class="code">TP</code> do not need to be marked
with <code class="code">GTY</code>.  The user needs to provide template functions to mark
all the fields of the type.
</p>
<p>The following code snippets represent all the functions that need to
be provided. Note that type <code class="code">TP</code> may reference to more than one
type. In these snippets, there is only one type <code class="code">T</code>, but there
could be more.
</p>
<div class="example smallexample">
<pre class="example-preformatted">template&lt;typename T&gt;
void gt_ggc_mx (TP&lt;T&gt; *tp)
{
  extern void gt_ggc_mx (T&amp;);

  /* This marks field 'fld' of type 'T'.  */
  gt_ggc_mx (tp-&gt;fld);
}

template&lt;typename T&gt;
void gt_pch_nx (TP&lt;T&gt; *tp)
{
  extern void gt_pch_nx (T&amp;);

  /* This marks field 'fld' of type 'T'.  */
  gt_pch_nx (tp-&gt;fld);
}

template&lt;typename T&gt;
void gt_pch_nx (TP&lt;T *&gt; *tp, gt_pointer_operator op, void *cookie)
{
  /* For every field 'fld' of 'tp' with type 'T *', call the given
     pointer operator.  */
  op (&amp;(tp-&gt;fld), NULL, cookie);
}

template&lt;typename T&gt;
void gt_pch_nx (TP&lt;T&gt; *tp, gt_pointer_operator, void *cookie)
{
  extern void gt_pch_nx (T *, gt_pointer_operator, void *);

  /* For every field 'fld' of 'tp' with type 'T', call the pointer
     walker for all the fields of T.  */
  gt_pch_nx (&amp;(tp-&gt;fld), op, cookie);
}
</pre></div>

<p>Support for user-defined types is currently limited. The following
restrictions apply:
</p>
<ol class="enumerate">
<li> Type <code class="code">TP</code> and all the argument types <code class="code">T</code> must be
marked with <code class="code">GTY</code>.

</li><li> Type <code class="code">TP</code> can only have type names in its argument list.

</li><li> The pointer walker functions are different for <code class="code">TP&lt;T&gt;</code> and
<code class="code">TP&lt;T *&gt;</code>. In the case of <code class="code">TP&lt;T&gt;</code>, references to
<code class="code">T</code> must be handled by calling <code class="code">gt_pch_nx</code> (which
will, in turn, walk all the pointers inside fields of <code class="code">T</code>).
In the case of <code class="code">TP&lt;T *&gt;</code>, references to <code class="code">T *</code> must be
handled by calling the <code class="code">op</code> function on the address of the
pointer (see the code snippets above).
</li></ol>

</div>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="GGC-Roots.html">Marking Roots for the Garbage Collector</a>, Previous: <a href="Inheritance-and-GTY.html">Support for inheritance</a>, Up: <a href="Type-Information.html">Memory Management and Type Information</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
